package edu.beyond.employment.company.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.beyond.employment.commom.util.AppUtil;
import edu.beyond.employment.company.dao.CompanyDao;
import edu.beyond.employment.company.dao.PositionDao;
import edu.beyond.employment.company.entity.CompanyEntity;
import edu.beyond.employment.company.entity.PositionEntity;

/**
 * CompanyServlet控制层
 */
public class CompanyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CompanyDao companyDao = new CompanyDao();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取页面传来的参数
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("login".equals(command)) {
			login(request, response);
		} else if ("main".equals(command)) {
			main(request, response);
		} else if ("logout".equals(command)) {
			logout(request, response);
		} else {
			throw new IllegalStateException("请求非法~");
		}
	}

	/**
	 * 公司职员登录Servlet
	 */
	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		// 获取页面传来的参数
		String code = request.getParameter("code");
		String password = request.getParameter("passwd");
		// 获取表单验证码
		String validateCode = request.getParameter("validate");
		// session里存放的验证码
		String sessionCode = (String) request.getSession().getAttribute("rand");
		if (!validateCode.equals(sessionCode)) {
			request.setAttribute("code", code);
			request.setAttribute("errorInfo", "验证码输入错误 ！");
			request.getRequestDispatcher("view/company/company-login.jsp").forward(request, response);
		}
		// 调用业务逻辑
		CompanyEntity company = companyDao.login(code, password);

		// 转向页面
		if (company == null) {
			request.setAttribute("code", code);
			request.setAttribute("errorInfo", "用户名或密码错误");
			request.getRequestDispatcher("view/company/company-login.jsp").forward(request, response);

		} else {
			request.getSession().setAttribute("company", company);
			// 重定向 -> 优点：地址上不会显示此次Servlet的请求地址！，缺点：不可以携带参数
			response.sendRedirect("CompanyServlet?command=main");

		}

	}

	/**
	 * main:一个中转servlet避免表单的重复提交
	 */
	private void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		// 1、获取参数
		int companyId = AppUtil.getCompanyId(request, response);
		// 调用业务逻辑
		List<PositionEntity> positionList = new PositionDao().listById(companyId);
		request.setAttribute("positionList", positionList);
		// 转向
		// request.getRequestDispatcher("ApplicationServlet?command=listCompanyVerifyByPage").forward(request,
		// response);
		request.getRequestDispatcher("view/company/main.jsp").forward(request, response);
	}

	/**
	 * 注销
	 */
	private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1、获取前台传过来的参数
		// 2、调用业务逻辑
		request.getSession().invalidate();
		// 3、转向
		request.getRequestDispatcher("view/company/company-login.jsp").forward(request, response);
	}

}
